package com.quyl.poi.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.quyl.poi.model.*;
import org.thymeleaf.context.Context;

import java.util.List;

/**
 * html数据渲染操作
 */
public class SetDataUtil {

    /**
     * 赋值操作
     *
     * @param cssStylePath
     * @param excelData
     * @return
     */
    public static Context copyValueForHtml(String cssStylePath, JSONObject excelData) {
        //解析html,并输出到指定文件夹中，Context是给thymeleaf模版提供变量的。
        Context context = new Context();
        //设置样式
        context.setVariable(Constant.CSS_STYLE_PATH, cssStylePath + Constant.STYLE_CSS);
        //表名
        context.setVariable(Constant.TABLE_NAME, excelData.get(Constant.TABLE_NAME));
        //基本信息
        String baseInfo = excelData.get(Constant.BASIC_INFO).toString();
        List<BaseInfo> baseInfoList = JSON.parseArray(baseInfo).toJavaList(BaseInfo.class);
        for (int i = 0; i < baseInfoList.size(); i++) {
            context.setVariable("bs_" + i, baseInfoList.get(i).getValue());
        }

        //模型定义
        String modelDefine = excelData.get(Constant.MODEL_DEFINE).toString();
        JSONObject jsonObject = JSON.parseObject(modelDefine);
        String modelData = jsonObject.get(Constant.LABEL_DATA).toString();
        List<ModelDefine> defineList = JSON.parseArray(modelData).toJavaList(ModelDefine.class);
        for (int i = 0; i < defineList.size(); i++) {
            context.setVariable("mf_label9_" + i, defineList.get(i).getLabel9());
            context.setVariable("mf_label10_" + i, defineList.get(i).getLabel10());
            context.setVariable("mf_label13_" + i, defineList.get(i).getLabel13());
            context.setVariable("mf_label16_" + i, defineList.get(i).getLabel16());
            context.setVariable("mf_label17_" + i, defineList.get(i).getLabel17());
            context.setVariable("mf_label18_" + i, defineList.get(i).getLabel18());
            context.setVariable("mf_label0_" + i, defineList.get(i).getLabel0());
        }
        //数据源
        String dataSource = excelData.get(Constant.DATA_SOURCE).toString();
        JSONObject jsonObject2 = JSON.parseObject(dataSource);
        String dataSourceValue = jsonObject2.get(Constant.LABEL_DATA).toString();
        List<DataSource> dataSourceList = JSON.parseArray(dataSourceValue).toJavaList(DataSource.class);
        for (int i = 0; i < dataSourceList.size(); i++) {
            context.setVariable("ds_label1_" + i, dataSourceList.get(i).getLabel1());
            context.setVariable("ds_label2_" + i, dataSourceList.get(i).getLabel2());
            context.setVariable("ds_label3_" + i, dataSourceList.get(i).getLabel3());
            context.setVariable("ds_label0_" + i, dataSourceList.get(i).getLabel0());
        }

        //算法描述
        String algorithmDesc = excelData.get(Constant.ALGORITHM_DESC).toString();
        JSONObject jsonObject3 = JSON.parseObject(algorithmDesc);
        String algorithmDescValue = jsonObject3.get(Constant.LABEL_DATA).toString();
        List<AlgorithmDesc> descList = JSON.parseArray(algorithmDescValue).toJavaList(AlgorithmDesc.class);
        for (int i = 0; i < descList.size(); i++) {
            context.setVariable("al_label6_" + i, descList.get(i).getLabel6());
            context.setVariable("al_label5_" + i, descList.get(i).getLabel5());
        }

        //校验规则
        String checkRule = excelData.get(Constant.CHECK_RULE).toString();
        JSONObject jsonObject4 = JSON.parseObject(checkRule);
        String checkRuleValue = jsonObject4.get(Constant.LABEL_DATA).toString();
        List<CheckRule> ruleList = JSON.parseArray(checkRuleValue).toJavaList(CheckRule.class);
        for (int i = 0; i < ruleList.size(); i++) {
            context.setVariable("cr_label9_" + i, ruleList.get(i).getLabel9());
            context.setVariable("cr_label8_" + i, ruleList.get(i).getLabel8());
        }

        //使用说明
        String useDesc = excelData.get(Constant.USER_DESC).toString();
        JSONObject jsonObject5 = JSON.parseObject(useDesc);
        String useDescValue = jsonObject5.get(Constant.LABEL_DATA).toString();
        List<UseDesc> useDescList = JSON.parseArray(useDescValue).toJavaList(UseDesc.class);
        for (int i = 0; i < useDescList.size(); i++) {
            context.setVariable("ud_label12_" + i, useDescList.get(i).getLabel12());
            context.setVariable("ud_label11_" + i, useDescList.get(i).getLabel11());
        }
        return context;
    }
}
